package com.petmall.controller;

import com.petmall.entity.Admin;
import com.petmall.entity.Message;
import com.petmall.entity.Order_items;
import com.petmall.entity.User;
import com.petmall.service.MessageService;
import com.petmall.service.OrderService;
import com.petmall.util.SessionAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pig2guang on 2017/5/2.
 */
@Controller
public class MessageContrller {

    @Autowired
    private MessageService messageservice;

    @Autowired
    private OrderService orderservice;

    @RequestMapping(value = "/promptSend", method= RequestMethod.GET)
    public String promptSend(@RequestParam("order_id") String order_id, HttpSession httpSession, Model model){
        messageservice.addOrderPromptMessage( order_id, httpSession.getAttribute(new SessionAttributes().user));
        ArrayList<Order_items> orders = orderservice.getUnSentUserOrders(httpSession.getAttribute(new SessionAttributes().user));
        model.addAttribute("orders", orders);
        return "my-unsent-order";
    }

    @RequestMapping(value = "/myMessage")
    public String getUserAllmessages(Model model, HttpSession httpSession){
        List<Message> messages = messageservice.getUserAllMessage( httpSession.getAttribute(new SessionAttributes().user) );
        System.out.print(messages.size());
        model.addAttribute("messages", messages);
        return "my-message";
    }

    @RequestMapping(value = "/messageManagement")
    public String manageAllmessages(HttpSession httpSession, Model model){
        if(httpSession.getAttribute(new SessionAttributes().admin) != null){
            List<Message> messages = messageservice.getAdminAllMessage();
            //System.out.print(messages.size());
            model.addAttribute("messages", messages);
            return "message-management";
        }else{
            model.addAttribute("admin", new Admin());
            model.addAttribute("user", new User());
            return "login";
        }
    }

    @RequestMapping(value = "/deleteMessage", method= RequestMethod.GET)
    public String promptSend(@RequestParam("message_id") String message_id, Model model){
        messageservice.deleteMessage(message_id);
        List<Message> messages = messageservice.getAdminAllMessage();
        System.out.print(messages.size());
        model.addAttribute("messages", messages);
        return "message-management";
    }

}
